CREATE TABLE [dbo].[tb_Documentacion]
(
[IdDocumentacion] [int] NOT NULL IDENTITY(1, 1),
[FechaEntrada] [datetime] NULL,
[OrganismoRemitente] [int] NULL CONSTRAINT [DF__tb_Docume__Organ__24CDD6EA] DEFAULT ((662)),
[NombreInstitucion] [varchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Firma] [varchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Motivo] [varchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Localidad] [int] NULL CONSTRAINT [DF__tb_Docume__Local__25C1FB23] DEFAULT ((21)),
[Observaciones] [varchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[NTUser] [int] NULL CONSTRAINT [DF_tb_Documentacion_NTUser] DEFAULT ([dbo].[sp_usuario](suser_sname())),
[FechaCarga] [datetime] NULL CONSTRAINT [DF_tb_Documentacion_FechaCarga] DEFAULT (getdate()),
[CodigoViejo] [int] NULL CONSTRAINT [DF__tb_Docume__Codig__26B61F5C] DEFAULT ([dbo].[sp_usuario](suser_sname())),
[ReparticiondeIngreso] [int] NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[IniciarDocumentacionMovimientos] ON [dbo].[tb_Documentacion] 
FOR INSERT
AS

 Declare @Documentacion int   Declare @FechaEntrada datetime     Declare @Origen int  

select 	@Documentacion=idDocumentacion,
	@FechaEntrada=FechaEntrada,
	@Origen=OrganismoRemitente
from inserted 


if (@Documentacion  is not null ) and  (@FechaEntrada is not null) 
begin
insert into tb_DocumentacionMovimientos
		(IdDocumentacion,
		FechaEntrada,
		Origen)
VALUES 
	(
	@Documentacion,
	@FechaEntrada,
	@Origen
	)	

end
GO
ALTER TABLE [dbo].[tb_Documentacion] ADD CONSTRAINT [PK_tb_Documentacion] PRIMARY KEY CLUSTERED  ([IdDocumentacion]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_Documentacion] WITH NOCHECK ADD CONSTRAINT [FK_tb_Documentacion_tb_Localidades] FOREIGN KEY ([Localidad]) REFERENCES [dbo].[tb_Localidades] ([idLocalidad])
GO
ALTER TABLE [dbo].[tb_Documentacion] WITH NOCHECK ADD CONSTRAINT [FK_tb_Documentacion_tb_SubOrganizaciones] FOREIGN KEY ([OrganismoRemitente]) REFERENCES [dbo].[tb_SubOrganizaciones] ([idSubOrganizacion])
GO
